<template>
    <!-- 订单详情对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible" :visible.sync="dialogVisible" width="30%" @close="handleClose">
        <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
            <el-form-item label="大类名称" prop="name">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="大类描述" prop="remark">
                <el-input v-model="form.remark" type="textarea" :rows="4"></el-input>
            </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
            <el-button @click="handleClose">取消</el-button>
            <el-button type="primary" @click="handleConfirm">确认</el-button>
        </div>
    </el-dialog>
</template>

<script>
export default {
    name: 'dialog',
    data() {
        return {
            form: {
                id: -1,
                name: '',
                remark: '',
            },
            rules: {    
                name: [
                    { required: true, message: '请输入商品大类名称', trigger: 'blur' }
                ],
                remark: [
                    { required: true, message: '请输入商品大类描述' },
                    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'change' }
                ]
            }
        }
    },
    props: ['dialogVisible', 'id', 'dialogTitle'],
    watch: {
        dialogVisible(val) {
            if (val != -1) {
                initFormData(val)
            } else { 
                this.$refs.formRef.resetFields();
                this.form = {
                    id: -1,
                    name: '',
                    remark:''
                }
            }
        }
    },
    methods: {
        handleClose() {
            this.$emit('click');
        },
        // 根据商品大类 id 获取商品大类信息
        async initFormData(id) {
            let result = await this.$API.categoty.reqInitFormData(id);
            if (result.code == 200) {
                this.form = result.data;    
            }
        },
        // 提交确认修改的商品大类数据
        async handleConfirm() {
            this.$refs.formRef.validate(async (valid) => {
                if (valid) {
                    let result = await this.$API.categoty.reqSaveFormData(this.form)
                    let data = result.data;
                    if (data.code == 0) {
                        // 表示提交成功
                        this.$message({
                            type: 'success',
                            message: '执行成功'
                        })
                        // 执行成功之后清空表单数据
                        this.$refs.formRef.resetFields();
                        // 子组件调用父组件中的方法 重新刷新数据
                        this.$emit('initBigTypeList');
                        this.handleClose();
                    } else {
                        this.$message({
                            type: 'success',
                            message: `${data.msg}`
                        })
                    }
                }
                else {
                    this.$message({
                        type: 'error',
                        message: '执行失败'
                    })
                }
            })
        },
    }
}

</script>

<style lang="scss" scoped></style>
